package clientApp;
import java.io.*;
import java.net.*;

/**
 * This is the main for the client side of network project 1. It uses a menu
 * object to display a menu to the user, get the choice of command, and the 
 * number of threads to create and ask for that command. it then creates 
 * that number of threads with a socket that has the server's ip address
 * (The server's ip address is the run argument)and the port which is 4444 and
 * the number of threads that need to be made. It waits till the times from
 * those threads have been added to the times array and then it redisplays
 * the menu.
 *      I have found one error on this side. It occured once when i tried to get
 *      the date with one thread. The error seemed to happen when a thread tried
 *      to add a time to the times object in main. Exception in thread "Thread-0"
 * java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: 
 * timeAvg.addTimeat clientApp.ThreadClient.run(ThreadClient.java:42)
 *      was the error coming out. I looked this up and there seemed only one way
 *      to stop this and that would be to delete the index under the cache and all
 *      the subdirectories and then restart netbeans. This error seemed to be tied
 *      to netbeans and not our code itelf. It only occured once and rerunning the 
 *      program worked completly fine.-R
 */

public class Main {
    static timeAvg times;
    static Menu mnu = new Menu();
    static int ThreadNum = 0;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        String ipAddress = new String();
        if (args.length != 0){
                ipAddress = args[0];
        BufferedReader stdIn = new BufferedReader(
                new InputStreamReader(System.in));
        String userInput;
        mnu.displayMenu();
        while ((userInput = stdIn.readLine()) != null) {
            if (mnu.validChoice(mnu.getReqNumber(userInput))) {
                ThreadNum = mnu.getThreadNum();
                times=new timeAvg(ThreadNum);
                if (ThreadNum != 0){
                for (int x = 0; x < ThreadNum; x++) {
                   try{
                        ThreadClient clientSocket = new ThreadClient(
                            new Socket(ipAddress, 4444),
                            mnu.getReqNumber(userInput));
                    clientSocket.start();
                   }
                   catch (ConnectException e){
                        System.err.println("Unable to establish connection. Program will now terminate...");
                        System.exit(1);
                   }
                }
                }
               if (ThreadNum != 0){
               while(times.getFull());
               times.display();
               }
            }
            if(userInput.equals("7")){
                System.out.println("Exit command invoked. Program will now exit...");
                break;
                }
            mnu.displayMenu();
        }    
        }
        else{
                System.out.println("Server IP address not specified. Program will now terminate...");
                System.exit(0);
               
        }

       
    } //  end main method
}

